import 'dart:core';

/// <summary>
/// author：wb
/// dateTime： 2021/8/15 3:02 下午
/// filePath： lib/util/time.dart
/// desc: 时间工具类
/// <summary>
///
class TimeUtil {

  /// 小于10 前面补0
  static String getDateStr(int time){
    return time<10?'0'+time.toString():time.toString();
  }

  /// 将 unix 时间戳转换为特定时间文本，如年月日
  static String convertTime(int timestamp) {
    DateTime msgTime = DateTime.fromMillisecondsSinceEpoch(timestamp);
    DateTime nowTime = DateTime.now();
    //同一年
    if (nowTime.year == msgTime.year) {
      //同一月
      if (nowTime.month == msgTime.month) {
        //同一天 时:分
        if (nowTime.day == msgTime.day) {
          return msgTime.hour.toString() + ":" + getMinute(msgTime.minute);
        } else {
          if (nowTime.day - msgTime.day == 1) {
            //昨天
            return "昨天";
          } else if (nowTime.day - msgTime.day < 7) {
            return getWeekday(msgTime.weekday);
          }
        }
      }
    }
    return msgTime.year.toString() + "/" + msgTime.month.toString() + "/" + msgTime.day.toString();
  }

  /// 将 unix 时间戳转换为特定时间文本，如年月日
  static String convertDateTime(int timestamp) {
    DateTime msgTime = DateTime.fromMillisecondsSinceEpoch(timestamp);
    String hour=getDateStr(msgTime.hour);
    String day=getDateStr(msgTime.day);
    String year=getDateStr(msgTime.year);
    String month=getDateStr(msgTime.month);
    String minute=getDateStr(msgTime.minute);
    String second=getDateStr(msgTime.second);
    String result=year+'-'+month+'-'+day+' '+hour+':'+minute;
    return result;
  }

  /// 小于10分钟 前面补0
  static String getMinute(int minute){
    return minute<10?'0'+minute.toString():minute.toString();
  }

  /// 是否需要显示时间，默认 相差 5 分钟
  static bool needShowTime(int sentTime1, int sentTime2,{int sent:5}) {
    if(sentTime1 == null || sentTime2 == null){
      return false;
    }
    return (sentTime1 - sentTime2).abs() > sent * 60 * 1000;
  }

  /// 星期几
  static String getWeekday(int weekday) {
    switch (weekday) {
      case 1:
        return "星期一";
      case 2:
        return "星期二";
      case 3:
        return "星期三";
      case 4:
        return "星期四";
      case 5:
        return "星期五";
      case 6:
        return "星期六";
      default:
        return "星期日";
    }
  }
}
